package com.smart.translator.ui.device;

import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.smart.translator.R;
import java.util.List;

public class HistoryDeviceAdapter extends RecyclerView.Adapter<HistoryDeviceAdapter.HistoryDeviceViewHolder> {
    public interface OnDeleteClickListener {
        void onDelete(String address);
    }

    public interface OnItemClickListener {
        void onItemClick(String address);
    }

    private List<BluetoothDevice> historyDevices;
    private OnDeleteClickListener deleteClickListener;
    private OnItemClickListener itemClickListener;

    public HistoryDeviceAdapter(List<BluetoothDevice> historyDevices, OnDeleteClickListener deleteClickListener, OnItemClickListener itemClickListener) {
        this.historyDevices = historyDevices;
        this.deleteClickListener = deleteClickListener;
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public HistoryDeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_device, parent, false);
        return new HistoryDeviceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryDeviceViewHolder holder, int position) {
        BluetoothDevice device = historyDevices.get(position);
        holder.tvName.setText(device.getName() != null ? device.getName() : "未知设备");
        holder.tvAddress.setText(device.getAddress());
        holder.btnDelete.setOnClickListener(v -> {
            if (deleteClickListener != null) {
                deleteClickListener.onDelete(device.getAddress());
            }
        });
        holder.itemView.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(device.getAddress());
            }
        });
    }

    @Override
    public int getItemCount() {
        return historyDevices.size();
    }

    public static class HistoryDeviceViewHolder extends RecyclerView.ViewHolder {
        TextView tvName, tvAddress;
        Button btnDelete;
        public HistoryDeviceViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_history_device_name);
            tvAddress = itemView.findViewById(R.id.tv_history_device_address);
            btnDelete = itemView.findViewById(R.id.btn_delete_history_device);
        }
    }
} 